import 'package:example/app/data/model/article_bean.dart';
import 'package:example/app/modules/main_module/home_page/logic.dart';
import 'package:example/app/utils/string_utils.dart';
import 'package:example/app/utils/url_utils.dart';
import 'package:example/app/widget/article_favourite_widget.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_base_framework/flutter_base_framework.dart';

class ArticleItem extends StatelessWidget {
  final int index;
  final ArticleBean item;
  final bool isTop;

  /// 列表项的点击事件
  final Function(ArticleBean item) onTap;

  /// 头像昵称的点击事件
  final Function(ArticleBean item)? onUserInfoClick;

  /// 标签的点击事件
  final Function(ArticleBean item)? onTagSuperClick;

  /// 二级标签点击事件
  final Function(ArticleBean item)? onTagChildClick;

  const ArticleItem(
      {Key? key,
      required this.index,
      required this.item,
      required this.onTap,
      this.isTop = false,
      this.onUserInfoClick,
      this.onTagSuperClick,
      this.onTagChildClick})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<HomePageLogic>(
      builder: (_) {
        return GestureDetector(
          onTap: () => onTap(item),
          child: Card(
            elevation: 6.dp,
            margin: EdgeInsets.only(top: 10.dp, left: 10.dp, right: 10.dp),
            child: Container(
              width: Get.infinity,
              padding: EdgeInsets.symmetric(horizontal: 10.dp, vertical: 10.dp),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(6.dp),
              ),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    children: [
                      GestureDetector(
                        onTap: onUserInfoClick == null ? null : onUserInfoClick!(item),
                        child: ImageView(
                          url: UrlUtils.randomUrl(
                              width: 20.dp.toInt(),
                              height: 20.dp.toInt(),
                              key: item.author?.isNotEmpty == true
                                  ? item.author!
                                  : item.shareUser!),
                          width: 20.dp,
                          height: 20.dp,
                          isCircular: true,
                        ),
                      ),
                      Get.widthBox(5.dp),
                      GestureDetector(
                        onTap: onUserInfoClick == null ? null : onUserInfoClick!(item),
                        child: Text(
                          '${item.author?.isNotEmpty == true ? item.author : item.shareUser!}',
                          style: TextStyle(color: Colors.grey, fontSize: 12.sp),
                        ),
                      ),
                      Spacer(),
                      Text(
                        '${item.niceDate}',
                        style: TextStyle(color: Colors.grey, fontSize: 12.sp),
                      ),
                    ],
                  ),
                  Get.heightBox(5.dp),
                  item.envelopePic?.isEmpty == true
                      ? Text(
                          '${StringUtils.removeHtmlLabel(item.title)}',
                          maxLines: 2,
                          overflow: TextOverflow.ellipsis,
                          style: TextStyle(color: '#333333'.c, fontSize: 14.sp),
                        )
                      : Container(
                          width: Get.infinity,
                          child: Row(
                            children: [
                              Expanded(
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Text(
                                      '${StringUtils.removeHtmlLabel(item.title)}',
                                      maxLines: 2,
                                      overflow: TextOverflow.ellipsis,
                                      style: TextStyle(
                                          color: '#333333'.c, fontSize: 14.sp),
                                    ),
                                    Get.heightBox(2.dp),
                                    Text(
                                      '${StringUtils.removeHtmlLabel(item.desc)}',
                                      maxLines: 1,
                                      overflow: TextOverflow.ellipsis,
                                      style: TextStyle(
                                          color: '#999999'.c, fontSize: 12.sp),
                                    )
                                  ],
                                ),
                              ),
                              Get.widthBox(5.dp),
                              ImageView.network(
                                url: item.envelopePic!,
                                width: 60.dp,
                                height: 60.dp,
                                borderRadius: BorderRadius.circular(6.dp),
                              ),
                            ],
                          ),
                        ),
                  Row(
                    children: [
                      /// 置顶
                      Visibility(
                        visible: isTop,
                        child: Container(
                          padding: EdgeInsets.symmetric(
                              horizontal: 5.dp, vertical: 3.dp),
                          margin: EdgeInsets.only(right: 5.dp),
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(3.dp),
                            border: Border.all(
                                color: Colors.blueAccent, width: 0.5.dp),
                            color: Colors.white,
                          ),
                          child: Center(
                            child: Text(
                              '置顶',
                              style: TextStyle(
                                  color: Colors.blueAccent, fontSize: 11.sp),
                            ),
                          ),
                        ),
                      ),

                      /// 标签
                      RichText(
                        text: TextSpan(children: [
                          TextSpan(
                              text: '${item.superChapterName}',
                              style: TextStyle(
                                  color: '#999999'.c, fontSize: 11.sp),
                            recognizer: TapGestureRecognizer()..onTap = onTagSuperClick == null ? null : onTagSuperClick!(item),
                          ),
                          TextSpan(
                              text: '·',
                              style: TextStyle(
                                  color: '#999999'.c, fontSize: 11.sp)),
                          TextSpan(
                              text: '${item.chapterName}',
                              style: TextStyle(
                                  color: '#999999'.c, fontSize: 11.sp),
                              recognizer: TapGestureRecognizer()..onTap = onTagChildClick == null ? null : onTagChildClick!(item)
                          ),
                        ]),
                      ),
                      Spacer(),

                      /// 收藏
                      ArticleFavouriteWidget(
                        articleBean: item,
                      )
                    ],
                  ),
                ],
              ),
            ),
          ),
        );
      },
    );
  }
}
